package action;

import Impl.userDaoImpl;
import com.sun.xml.internal.bind.v2.model.core.ID;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "UserOpt")
public class UserOpt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------正在执行UserOpt-----------");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
//        System.out.println(method);
        userDaoImpl userDao =new userDaoImpl();
        if (method!=null && method.equals("del")){
            int uid = Integer.parseInt(request.getParameter("uid"));
            userDao.deleteUser(uid);
            System.out.println("正在删除用户");
        }else if(method!=null && method.equals("search")){
            System.out.println("正在查找用户");
            String condition = request.getParameter("search_condtion");
            System.out.println(condition);
            List<User> userList=userDao.findUserByCondition(condition);
            request.setAttribute("userlist",userList);
            request.setAttribute("page", "no");
            request.setAttribute("allcount",userList.size());
            request.getRequestDispatcher("/member-list.jsp").forward(request,response);
        }
        else{
            System.out.println("正在修改用户密码");
            int uid = Integer.parseInt(request.getParameter("uid"));
            String pwd  =request.getParameter("newpassword");
            System.out.println(pwd);
            if (pwd!=null){
                userDao.updateUserPwd(uid,pwd);
            }
        }
        PrintWriter out = response.getWriter();
        out.println("删除成功");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
